package features;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import utils.LineRegex;
import utils.Pair;
import utils.TagCluster;
import utils.TagClustering;
import utils.Utils;
import weka.core.Attribute;

public class LabelExtractor {
	
	private String cluster_name;
	private TagClustering tag_clustering;
	
	public LabelExtractor(TagClustering tag_clustering) {
		super();
		this.tag_clustering = tag_clustering;
	}

	public String getCluster_name() {
		return cluster_name;
	}

	public void setCluster_name(String cluster_name) {
		this.cluster_name = cluster_name;
	}

	public int[] getClassFeatures(String line,Pair<double[], int[]> vals_idxs) {
		int res[] = null;
		if ( cluster_name.equals("") ) {
			res = new int[tag_clustering.getNumClusters()];
			for ( String cluster :  LineRegex.getClusters(line,tag_clustering) ) {
				res[tag_clustering.getClusterIdx(cluster)] = 1;
			}
		}
		else {
			TagCluster cluster = tag_clustering.getCluster(cluster_name);
			res = new int[cluster.getNumTags()];
			for ( String tag : LineRegex.getRelevantTags(line,tag_clustering) ) {
				if ( cluster.contains(tag) ) 
					res[cluster.getIdxFor(tag)] = 1;
			}
		}
		return res;	
	}
	
	public List<Attribute> getClassAttributes() {
		ArrayList<Attribute> res = new ArrayList<Attribute>();
		if ( cluster_name.equals("") ) {
			for ( String cluster : tag_clustering.getAllClusterNames() ) 
				res.add(new Attribute("Cluster_"+cluster,Utils.vals_01));
		}
		else {
			TagCluster cluster = tag_clustering.getCluster(cluster_name);
			for ( String tag : cluster.getTags() ) 
				res.add(new Attribute("Tag_"+tag,Utils.vals_01));
		}
		return res;
	}

	public List<Pair<Double, Integer>> getClassFeatures(
			String line, int numFeatures) {
		List<Pair<Double,Integer>> res = new LinkedList<Pair<Double,Integer>>();
		if ( cluster_name.equals("") ) {
			for ( String cluster :  LineRegex.getClusters(line,tag_clustering) ) {
				res.add(new Pair<Double,Integer>(1.0,numFeatures+tag_clustering.getClusterIdx(cluster)));
			}
		}
		else {
			TagCluster cluster = tag_clustering.getCluster(cluster_name);
			for ( String tag : LineRegex.getRelevantTags(line,tag_clustering) ) {
				if ( cluster.contains(tag) ) 
					res.add(new Pair<Double,Integer>(1.0,numFeatures+cluster.getIdxFor(tag)));
			}
		}
		return res;
	}

	public static LabelExtractor getLabelExtractor(TagClustering tag_clustering,String file_name) {
		LabelExtractor res =  new LabelExtractor(tag_clustering);
		res.setCluster_name(Utils.getCluster(file_name));
		return res;		
	}

}
